package com.ruint.master.view;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruint.master.R;

public class Topbar extends RelativeLayout implements TopbarConstantPool {
  private Context context;
  private TextView titleView;
  private ImageView rightButton;
  private ImageView btnExtra;// 搜索、搜索等

  public Topbar(Context context) {
    super(context);
    this.context = context;
    init();
  }

  public Topbar(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    init();
  }

  private void init() {
    LayoutInflater.from(context).inflate(R.layout.top_layout, this);
    titleView = (TextView) findViewById(R.id.title);
    rightButton = (ImageView) findViewById(R.id.right_button);
    btnExtra = (ImageView) findViewById(R.id.btn_extra);
    findViewById(R.id.back).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        ((Activity) context).onBackPressed();
      }
    });
    makeCorrespondenceMap();
  }

  public ImageView getLeftButton() {
    return (ImageView) findViewById(R.id.back);
  }

  public Topbar setTitle(String title) {
    titleView.setText(title);
    return this;
  }

  public Topbar setTitle(int resId) {
    titleView.setText(resId);
    return this;
  }

  @SuppressWarnings("unchecked")
  @Override
  public Topbar setRightButton(int resId) {
    rightButton.setVisibility(View.VISIBLE);
    fetchRightBtn(getRightButton(), getResources().getString(resId));
    return this;
  }

  public void leftPerfromClick() {
    findViewById(R.id.back).performClick();
  }

  public ImageView getExtraBtn() {
    return btnExtra;
  }

  public Topbar setExtraBtn(String text) {
    fetchRightBtn(getExtraBtn(), text);
    return this;
  }

  public Topbar setExtraBtnListener(OnClickListener listener) {
    btnExtra.setVisibility(View.VISIBLE);
    btnExtra.setOnClickListener(listener);
    return this;
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (KeyEvent.KEYCODE_BACK == keyCode) {
      findViewById(R.id.back).performClick();
      return true;
    }
    return super.onKeyDown(keyCode, event);
  }

  public ImageView getRightButton() {
    return rightButton;
  }

  public Topbar setLeftListener(OnClickListener listener) {
    findViewById(R.id.back).setOnClickListener(listener);
    return this;
  }

  public Topbar setRightListener(OnClickListener listener) {
    findViewById(R.id.right_button).setOnClickListener(listener);
    return this;
  }

  @Override
  public void fetchRightBtn(ImageView view, String text) {
    view.setImageResource(correspondecneMap.get(text));
  }

  @Override
  public Map<String, Integer> makeCorrespondenceMap() {
    correspondecneMap.put(TOPBAR_BUTTON_SUBMIT, TOPBAR_BUTTON_SUBMIT_ID);
    correspondecneMap.put(TOPBAR_BUTTON_CREATE_DIARY, TOPBAR_BUTTON_CREATE_DIARY_ID);
    correspondecneMap.put(TOPBAR_BUTTON_COLLECT, TOPBAR_BUTTON_COLLECT_ID);
    correspondecneMap.put(TOPBAR_BUTTON_SCREEN, TOPBAR_BUTTON_SCREEN_ID);
    correspondecneMap.put(TOPBAR_BUTTON_SEARCH, TOPBAR_BUTTON_SEARCH_ID);
    correspondecneMap.put(TOPBAR_BUTTON_HELP, TOPBAR_BUTTON_HELP_ID);
    correspondecneMap.put(TOPBAR_BUTTON_ILLUSTATE, TOPBAR_BUTTON_ILLUSTATE_ID);
    correspondecneMap.put(TOPBAR_BUTTON_MORE, TOPBAR_BUTTON_MORE_ID);
    correspondecneMap.put(TOPBAR_BUTTON_SAVE, TOPBAR_BUTTON_SAVE_ID);
    correspondecneMap.put(TOPBAR_BUTTON_CONCERN, TOPBAR_BUTTON_CONCERN_ID);
    correspondecneMap.put(TOPBAR_BUTTON_UNCONCERN, TOPBAR_BUTTON_UNCONCERN_ID);
    correspondecneMap.put(TOPBAR_BUTTON_COLLECTED, TOPBAR_BUTTON_COLLECTED_ID);
    return correspondecneMap;
  }

  @SuppressWarnings("unchecked")
  @Override
  public Topbar setRightButton(String text) {
    rightButton.setVisibility(View.VISIBLE);
    fetchRightBtn(getRightButton(), text);
    return this;
  }

}
